  
var Player_x_tile;
var Player_y_tile;
var Player_x;
var Player_y;
var Player_layer;
var Battle = new Object;
var Monster1 = new Object;
var Monster2 = new Object;
var Monster3 = new Object;
var Monster4 = new Object;

Monster1.posx = 0;
Monster1.posy = 0;
Monster2.posx = 0;
Monster2.posy = 0;
Monster3.posx = 0;
Monster3.posy = 0;
Monster4.posx = 0;
Monster4.posy = 0;

Battle.prevent = false;
Battle.Talk = false;
Battle.MiscExit = 0;

Monster1.agi = 0;
Monster2.agi = 0;
Monster3.agi = 0;
Monster4.agi = 0;
Monster1.hp = 0;
Monster2.hp = 0;
Monster3.hp = 0;
Monster4.hp = 0;
Monster1.exp = 0;
Monster2.exp = 0;
Monster3.exp = 0;
Monster4.exp = 0;
Monster1.gold = 0;
Monster2.gold = 0;
Monster3.gold = 0;
Monster4.gold = 0;

Battle.monsternum = 0;
Battle.gold = 0;
Battle.runable = true;
Battle.counter = 0;

function GetintoBattle(areathing, delay)
{
Battle.counter = 0;
Battle.engage = true;
Player.engagetemp = areathing;
Battle.engagemath = Math.random()*delay*Player.engage+delay*Player.engage;
SetUpdateScript("RandomBattleCheck();");
}

function RandomBattleCheck()
{
	if (Battle.engage == true)
	{
		if (GetPersonX("main character") != Battle.px || GetPersonY("main character") != Battle.py)
		{
			Battle.px = GetPersonX("main character");
			Battle.py = GetPersonY("main character"); 
			Battle.counter++;
		}

		if (Battle.counter >= Battle.engagemath)
		{
			Battle.counter = 0;
			Battle.engage = false;
			Battle.px = -1;
			Battle.py = -1;
			BindKey(KEY_ESCAPE, "GameMenu()", "");
			EngageBattle(Player.engagetemp);
		}

		while (AreKeysLeft())
		{	
			if (GetKey() == KEY_ESCAPE)
			{
				eatKey();
				GameMenu();
			}
		}
	}
}

function EngageBattle(area)
{
Battle.engage = false;
Battle.counter = 0;
Player.temp2 = ""
  if (Battle.prevent == false)
  { 
  BindKey(KEY_ESCAPE, "GoToActivePlayer()", "GoToActivePlayer()");
  Player_x = GetPersonX("main character");
  Player_y = GetPersonY("main character");

  Player_x_tile = Math.floor(Player_x/16);
  Player_y_tile = Math.floor(Player_y/16);
  Player_layer = GetPersonLayer("main character");

  Player.fightarea = area;
  Player.temp = GetTile(Player_x_tile, Player_y_tile, 0);

    if (Player.fightarea == "Two Rats")
    {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Build.rmp');
      BattleTypes("Two Rats");   
    }
    else if (Player.fightarea == "Three Rats")
    {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Build.rmp');
      BattleTypes("Three Rats");   
    }
    else if (Player.fightarea == "area1")
    {
      if (Player.temp == 1)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Grass.rmp');
      BattleTypes("Grass1");
      }
      else if (Player.temp == 2)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Forest.rmp');
      BattleTypes("Forest1");
      }
      else if (Player.temp == 3)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Grass.rmp');
      BattleTypes("Grass2");
      }
      else if (Player.temp == 4)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Grass.rmp');
      BattleTypes("Grass3");
      }
      else if (Player.temp == 5)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Forest.rmp');
      BattleTypes("Forest2");
      }
      else if (Player.temp == 6)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Grass.rmp');
      BattleTypes("Grass4");
      }
      else if (Player.temp == 7)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Forest.rmp');
      BattleTypes("Forest3");
      }
      else if (Player.temp == 9)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Grass.rmp');
      BattleTypes("Grass5");
      }
      else if (Player.temp == 10)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Grass.rmp');
      BattleTypes("Grass6");
      }
      else if (Player.temp == 8)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Forest.rmp');
      BattleTypes("Forest4");
      }
      else
      {
      Player.temp2 = "I don't know."
      BindKey(KEY_ESCAPE, "GameMenu()", "");
      GetintoBattle("area1", 3);
      }
    }
    if (Player.fightarea == "PireForest1")
    {
      if (Player.temp == 1)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Forest.rmp');
      BattleTypes("PireForest1");
      }
      else if (Player.temp == 2)
      {
      RenderMap();
      FadeToColor(1000, White);
      ChangeMap('Forest.rmp');
      BattleTypes("PireForest2");
      }
      else
      {
      Player.temp2 = "I don't know."
      Talk_Window("This is " + Player.temp2, Player.port);
      }
    }
    if (Player.fightarea == "PireForestBoss")
    {
    RenderMap();
    FadeToColor(1000, White);
    ChangeMap('Forest.rmp');
    BattleTypes("PireForestBoss");
    }
    if (Player.fightarea == "PangaraCave1")
    {
    RenderMap();
    FadeToColor(1000, White);
    ChangeMap('Cave.rmp');
    BattleTypes("PangaraCave1");
    }
    if (Player.fightarea == "PangaraCave1Boss")
    {
    RenderMap();
    FadeToColor(1000, White);
    ChangeMap('Cave.rmp');
      if (Player.event16 == 1)
      {
      Player.event16 = 2;
      }
      else
      {
      Player.event16 = 4;
      }
    BattleTypes("PangaraCave1Boss");
    }
    MakeMonsters();

    Player.dir = GetPersonDirection("main character");

    if (Player.temp2 == "I don't know.")
    {
    }
    else
    {
    Delay(200);
    SetPersonX("main character", 584);
    SetPersonY("main character", 184);
    Player.P1x = 584;
    Player.P1y = 184;
    SetPersonLayer("main character", 0);
      if (Player.hp <= 0)
      {
      SetPersonDirection("main character", 9);
      SetPersonFrame("main character", 0);
      }
      else if (Player.hp <= Player.maxhp/4)
      {
      SetPersonDirection("Player", 9);
      SetPersonFrame("Player", 1);
      }
      else
      {
      SetPersonDirection("main character", 6);
      SetPersonFrame("main character", 0);
      }
      CreatePerson("Weapon", "Weapon.rss", true);  
      CreatePerson("Weapon1", "Weapon1.rss", true);  
      CreatePerson("Weapon2", "Weapon2.rss", true);  
      CreatePerson("blank", "blank.rss", true);  
  
      if (Player2.pres == true)
      {
      CreatePerson("Player2", Player2.spriteset, true);  
      SetPersonX("Player2", 584);
      SetPersonY("Player2", 224);
      Player.P2x = 584;
      Player.P2y = 224;
      SetPersonLayer("Player2", 0);

        if (Player2.hp <= 0)
        {
        SetPersonDirection("Player2", 9);
        SetPersonFrame("Player2", 0);
        }
        else if (Player2.hp <= Player2.maxhp/4)
        {
        SetPersonDirection("Player2", 9);
        SetPersonFrame("Player2", 1);
        }
        else
        {
        SetPersonDirection("Player2", 6);
        SetPersonFrame("Player2", 0);
        }
      }
    
      if (Player3.pres == true)
      {
      CreatePerson("Player3", Player3.spriteset, true);  
      SetPersonX("Player3", 584);
      SetPersonY("Player3", 264);
      Player.P3x = 584;
      Player.P3y = 264;
      SetPersonLayer("Player3", 0);

        if (Player3.hp <= 0)
        {
        SetPersonDirection("Player3", 9);
        SetPersonFrame("Player3", 0);
        }
        else if (Player3.hp <= Player3.maxhp/4)
        {
        SetPersonDirection("Player3", 9);
        SetPersonFrame("Player3", 1);
        }
        else
        {
        SetPersonDirection("Player3", 6);
        SetPersonFrame("Player3", 0);
        }
      }

    DetachInput();
    DetachCamera();
    RenderMap();
    FadeFromColor(1000, White);

    BattleMenuPlayer1();  
    }
  }
  else
  {
  }
}  

function MakeMonsters()
{
Battle.counter = 0;
Monster1.stun = 0;
Monster2.stun = 0;
Monster3.stun = 0;
Monster4.stun = 0;

Player.modreg = false;
Player2.modreg = false;
Player3.modreg = false;
Player.modmpreg = false;
Player2.modmpreg = false;
Player3.modmpreg = false;
Player.moddef = false;
Player2.moddef = false;
Player3.moddef = false;

  if (Battle.monsternum == 1)
  {
  CreatePerson("Monster1", Monster1.sprite, true);  
  SetPersonX("Monster1", 200);
  SetPersonY("Monster1", 200);
  SetPersonLayer("Monster1", 0);
  Monster1.posx = 200;
  Monster1.posy = 200;
  }    
  else if (Battle.monsternum == 2)
  {

  CreatePerson("Monster1", Monster1.sprite, true);  
  SetPersonX("Monster1", 100);
  SetPersonY("Monster1", 175);
  SetPersonLayer("Monster1", 0);
  Monster1.posx = 100;
  Monster1.posy = 175;


  CreatePerson("Monster2", Monster2.sprite, true);  
  SetPersonX("Monster2", 100);
  SetPersonY("Monster2", 275);
  SetPersonLayer("Monster2", 0);
  Monster2.posx = 100;
  Monster2.posy = 275;

  }    
  else if (Battle.monsternum == 3)
  {
  CreatePerson("Monster1", Monster1.sprite, true);  
  SetPersonX("Monster1", 100);
  SetPersonY("Monster1", 175);
  SetPersonLayer("Monster1", 0);
  Monster1.posx = 100;
  Monster1.posy = 175;

  CreatePerson("Monster2", Monster2.sprite, true);  
  SetPersonX("Monster2", 100);
  SetPersonY("Monster2", 275);
  SetPersonLayer("Monster2", 0);
  Monster2.posx = 100;
  Monster2.posy = 275;

  CreatePerson("Monster3", Monster3.sprite, true);  
  SetPersonX("Monster3", 200);
  SetPersonY("Monster3", 225);
  SetPersonLayer("Monster3", 0);
  Monster3.posx = 200;
  Monster3.posy = 225;
  }    
  else if (Battle.monsternum == 4)
  {

  CreatePerson("Monster1", Monster1.sprite, true);  
  SetPersonX("Monster1", 100);
  SetPersonY("Monster1", 225);
  SetPersonLayer("Monster1", 0);
  Monster1.posx = 100;
  Monster1.posy = 225;

  CreatePerson("Monster2", Monster2.sprite, true);  
  SetPersonX("Monster2", 175);
  SetPersonY("Monster2", 175);
  SetPersonLayer("Monster2", 0);
  Monster2.posx = 175;
  Monster2.posy = 175;

  CreatePerson("Monster3", Monster3.sprite, true);  
  SetPersonX("Monster3", 175);
  SetPersonY("Monster3", 275);
  SetPersonLayer("Monster3", 0);
  Monster3.posx = 175;
  Monster3.posy = 275;

  CreatePerson("Monster4", Monster4.sprite, true);  
  SetPersonX("Monster4", 250);
  SetPersonY("Monster4", 225);
  SetPersonLayer("Monster4", 0);
  Monster4.posx = 250;
  Monster4.posy = 225;
  }    
}

function BattleEnd()
{
Player.str = Player.tempystr
Player2.str = Player2.tempystr
Player3.str = Player3.tempystr

Player.P1x = 650;
Player.P1y = 490;
Player.P2x = 650;
Player.P2y = 490;
Player.P3x = 650;
Player.P3y = 490;

if (Player.tournamentpres == false)
{
  RenderMap();
  FadeToColor(1000, White);

  if (Player.fightarea == "area1")
  {
    Player.map = "world_map.rmp";
  }
  else if (Player.fightarea == "PireForest1")
  {
    Player.map = "Pire_Forest.rmp";
  }
  else if (Player.fightarea == "PireForestBoss")
  {
    Player.map = "Pire_Forest.rmp";
  }
  else if (Player.fightarea == "Two Rats")
  {
    Player.map = "TorkApp.rmp";
  }
  else if (Player.fightarea == "Three Rats")
  {
    Player.map = "TorkApp.rmp";
  }
  else if (Player.fightarea == "PangaraCave1")
  {
    Player.map = "PangaraCave1.rmp";
  }


ChangeMap(Player.map);
SetPersonX("main character", Player_x);
SetPersonY("main character", Player_y);
SetPersonLayer("main character", Player_layer);
SetPersonDirection("main character", Player.dir);
AttachInput("main character");
AttachCamera("main character");
Delay(200);
SetCameraX(Player_x);
SetCameraY(Player_y);
  if (Battle.SpecialExit == true)
  {
  BattleSpecialExit();
  }
RenderMap();
FadeFromColor(1000, White);
}

  if (Battle.monsternum == 0)
  {
    GainExp();
    GainItem();
  }
  else
  {
    GainNoExp();
  }

Monster1.agi = 0;
Monster2.agi = 0;
Monster3.agi = 0;
Monster4.agi = 0;
Monster1.hp = 0;
Monster2.hp = 0;
Monster3.hp = 0;
Monster4.hp = 0;
Monster1.exp = 0;
Monster2.exp = 0;
Monster3.exp = 0;
Monster4.exp = 0;
Monster1.gold = 0;
Monster2.gold = 0;
Monster3.gold = 0;
Monster4.gold = 0;

Player.attack = Player.str + Player.weaponeffect;
Player.defence = Player.vit + Player.armoureffect;
Player.evade = Math.floor(Player.agi/5);

Player2.attack = Player2.str + Player2.weaponeffect;
Player2.defence = Player2.vit + Player2.armoureffect;
Player2.evade = Math.floor(Player2.agi/5);

Player3.attack = Player3.str + Player3.weaponeffect;
Player3.defence = Player3.vit + Player3.armoureffect;
Player3.evade = Math.floor(Player3.agi/5);

GarbageCollect();
BindKey(KEY_ESCAPE, "GameMenu()", "");
eatKey();

  if (Battle.MiscExit == 1)
  {
  Battle.MiscExit = 0;
  Talk_Window("Bill: Wait!  There's more!", "blank.png");
  Delay(200);
  EngageBattle("Three Rats");  
  }
  if (Battle.MiscExit == 2)
  {
  Battle.prevent = true;
	Battle.engage = false;
  Battle.MiscExit = 0;
  Delay(500);
  NewMap("TorkMain.rmp", 936, 936, 1, 0);
  Delay(200);
  Talk_Window("Bill: Oh thank you so much.  Your kindness is without equal as well as your fighting skills.  This shall go down in the history books as one of the fiercest battles ever fought!  Here's 10 Gp, spend it wisely.", "blank.png");
  Player.gp = Player.gp + 10;
  Player.event7 = 1;
  Delay(200);
  }

  if (Player.tournamentpres == true)
  {
  Battle.engage = false;
  TournamentStop1("Win");
  }
}
function GainExp()
{
Battle.gold = 0;
  Player.active = "No One";
  Battle.playernum = 1;
  Battle.totalexp = Monster1.exp + Monster2.exp + Monster3.exp + Monster4.exp;
  if (Player2.pres == true)
  {
    Battle.playernum = Battle.playernum + 1;
  }
  if (Player3.pres == true)
  {
    Battle.playernum = Battle.playernum + 1;
  }
  if (Player.hp == 0)
  {
    Battle.playernum = Battle.playernum - 1;
  }
  if (Player2.hp == 0)
  {
    Battle.playernum = Battle.playernum - 1;
  }
  if (Player3.hp == 0)
  {
    Battle.playernum = Battle.playernum - 1;
  }

Battle.gold = Monster1.gold + Monster2.gold + Monster3.gold + Monster4.gold;  
Battle.eachexp = Math.floor(Battle.totalexp/Battle.playernum);

if (Player.chrclass == "Ranger")
{
Battle.gold = Math.floor(Battle.gold*1.2);
}
if (Player2.chrclass == "Ranger")
{
Battle.gold = Math.floor(Battle.gold*1.2);
}
if (Player3.chrclass == "Ranger")
{
Battle.gold = Math.floor(Battle.gold*1.2);
}

Player.gp = Player.gp + Battle.gold;

 RenderMap() 
 WindowStyle.drawWindow (25, 300, 590, 155);
 Portrait = LoadImage(Player.port);
 Portrait.blit(30, 305);
 if (Player.hp == 0)
 {
 Font.drawTextBox (75, 315, 500, 100, 5, Player.name + " Gained 0 Exp");
 }
 else
 {
 Font.drawTextBox (75, 315, 500, 100, 5, Player.name + " Gained " + Battle.eachexp + " Exp");
 }
 
if (Player2.pres == true)
{
 Portrait = LoadImage(Player2.port);
 Portrait.blit(30, 345);

 if (Player2.hp == 0)
 {
 Font.drawTextBox (75, 360, 500, 100, 5, Player2.name + " Gained 0 Exp");
 }
 else
 {
 Font.drawTextBox (75, 360, 500, 100, 5, Player2.name + " Gained " + Battle.eachexp + " Exp");
 }
}
if (Player3.pres == true)
{
 Portrait = LoadImage(Player3.port);
 Portrait.blit(30, 390);
 if (Player3.hp == 0)
 {
 Font.drawTextBox (75, 405, 500, 100, 5, Player3.name + " Gained 0 Exp");
 }
 else
 {
 Font.drawTextBox (75, 405, 500, 100, 5, Player3.name + " Gained " + Battle.eachexp + " Exp");
 }
}

 Font.drawTextBox (30, 435, 500, 100, 5, "The Party Gained " + Battle.gold + " Gold");
eatKey();
FlipScreen();
GetKey();

  if (Player.hp > 0)
  {
    Player1GainExp(Battle.eachexp);
  }
  if (Player2.pres == true)
  {
    if (Player2.hp > 0)
    {
      Player2GainExp(Battle.eachexp);
    }
  }
  if (Player3.pres == true)
  {
    if (Player3.hp > 0)
    {
      Player3GainExp(Battle.eachexp);
    }
  }
eatKey();
}
function GainNoExp()
{
Talk_Window("No experience or gold was gained by the party", "blank.png");
Player.active = "No One";
eatKey();
} 

function GainItem()
{

  if (Math.random() <= Battle.ItemChance)
  {
  
  if (Battle.Item == "Potion")
  {
  Player.items.potion = Player.items.potion + 1;
  Talk_Window("Received a Potion!", "blank.png");
  }
  else if (Battle.Item == "Hi-Potion")
  {
  Player.items.hipotion = Player.items.hipotion + 1;
  Talk_Window("Received a Hi-Potion!", "blank.png");
  }
  else if (Battle.Item == "Full-Potion")
  {
  Player.items.fullpotion = Player.items.fullpotion + 1;
  Talk_Window("Received a Full-Potion!", "blank.png");
  }
  else if (Battle.Item == "Ether")
  {
  Player.items.ether = Player.items.ether + 1;
  Talk_Window("Received an Ether!", "blank.png");
  }
  else if (Battle.Item == "Turbo Ether")
  {
  Player.items.turboether = Player.items.turboether + 1;
  Talk_Window("Received a Turbo Ether!", "blank.png");
  }
  else if (Battle.Item == "Elixer")
  {
  Player.items.elixer = Player.items.elixer + 1;
  Talk_Window("Received an Elixer!", "blank.png");
  }
  else if (Battle.Item == "Tent")
  {
  Player.items.tent = Player.items.tent + 1;
  Talk_Window("Received a Tent!", "blank.png");
  }
  else if (Battle.Item == "Life")
  {
  Player.items.life = Player.items.life  + 1;
  Talk_Window("Received a Life!", "blank.png");
  }
  else if (Battle.Item == "Strange Ore")
  {
  Talk_Window("There seems to be some strange ore here.", Player.port);
  Delay(200);
  Talk_Window("Picked up some strange ore", "blank.png");
  }
  else
  {
  }

  }
} 
function GoToActivePlayer()
{
  if (Player.active == "Player1")
  {
    BattleMenuPlayer1();
  }
  else if (Player.active == "Player2")
  {
    BattleMenuPlayer2();
  }
  else if (Player.active == "Player3")
  {
    BattleMenuPlayer3(); 
  }
}  
function DrawPlayerStats()
{
      WindowStyle.drawWindow(250, 333, 385, 142);
      Font.drawText(252, 335, Player.name + ": " + Player.hp + "/" + Player.maxhp + " Mp: " + Player.mp + "/" + Player.maxmp);
      if (Player2.pres == true)
      {
        Font.drawText(252, 355, Player2.name + ": " + Player2.hp + "/" + Player2.maxhp + " Mp: " + Player2.mp + "/" + Player2.maxmp);
      }
      if (Player3.pres == true)
      {
        Font.drawText(252, 375, Player3.name + ": " + Player3.hp + "/" + Player3.maxhp + " Mp: " + Player3.mp + "/" + Player3.maxmp);
      }
}

function BattleName()
{
  if (Player.active == "Player1")
  {
  WindowStyle.drawWindow(5, 333, 235, 12);
  Font.drawText(7, 335, Player.name);
  }
  else if (Player.active == "Player2")
  {
  WindowStyle.drawWindow(5, 333, 235, 12);
  Font.drawText(7, 335, Player2.name);
  }
  else if (Player.active == "Player3")
  {
  WindowStyle.drawWindow(5, 333, 235, 12);
  Font.drawText(7, 335, Player3.name);
  }
  else if (Player.active == "Monster1")
  {
  WindowStyle.drawWindow(5, 333, 235, 12);
  Font.drawText(7, 335, Monster1.name);
  }
  else if (Player.active == "Monster2")
  {
  WindowStyle.drawWindow(5, 333, 235, 12);
  Font.drawText(7, 335, Monster2.name);
  }
    else if (Player.active == "Monster3")
  {
  WindowStyle.drawWindow(5, 333, 235, 12);
  Font.drawText(7, 335, Monster3.name);
  }
  else if (Player.active == "Monster4")
  {
  WindowStyle.drawWindow(5, 333, 235, 12);
  Font.drawText(7, 335, Monster4.name);
  }
}

function IsPlayerDead()
{
//Talk_Window("Test", "blank.png");
Player.dead = 0;
  if (Player.hp == 0)
  {
  Player.dead = Player.dead + 1;
  }
  if (Player2.pres == false)
  {
  Player.dead = Player.dead + 1;
  }
  else if (Player2.hp == 0)
  {
  Player.dead = Player.dead + 1;
  }
  if (Player3.pres == false)
  {
  Player.dead = Player.dead + 1;
  }
  else if (Player3.hp == 0)
  {
  Player.dead = Player.dead + 1;
  }
}
function GameOver()
{
Battle.counter = 0;
Battle.engage = false;
Player.P1x = 650;
Player.P1y = 490;
Player.P2x = 650;
Player.P2y = 490;
Player.P3x = 650;
Player.P3y = 490;

  if (Player.tournamentpres == true)
  {
  SetPersonDirection("main character", 4);
  TournamentStop1("Lose");
  }
  else
  {
  Player.MapEnginePres = true;
  ChangeMusic("");
  Talk_Window("Your Party has died", "blank.png");
  RenderMap();
  FadeToColor(1000, Black);

  DestroyPerson("main character");
  //ExitMapEngine();  
  ChangeMusic("");
  BindKey(KEY_ESCAPE, "", "");
  MainMenu();
  }
}



function IsMonster1Dead()
{
  if (Monster1.hp <= 0)
  {
  Battle.monsternum = Battle.monsternum - 1
  Delay(400);
  Destroy("Monster1");
  RenderMap();
  }
}
function IsMonster2Dead()
{
  if (Monster2.hp <= 0)
  {
  Battle.monsternum = Battle.monsternum - 1
  Delay(400);
  Destroy("Monster2");
  RenderMap();
  }
}
function IsMonster3Dead()
{
  if (Monster3.hp <= 0)
  {
  RenderMap();
  Battle.monsternum = Battle.monsternum - 1
  Delay(400);
  Destroy("Monster3");
  RenderMap();
  }
}
function IsMonster4Dead()
{
  if (Monster4.hp <= 0)
  {
  Battle.monsternum = Battle.monsternum - 1
  RenderMap();
  Delay(400);  
  Destroy("Monster4");
  RenderMap();
  }
}

function ShowHeathGain(hpgain)
{
RenderMap();
  if (hpgain >= 0)
  {
    if (Player.playertemp == "Player1")
    {
    HpFont.drawTextBox (Player.P1x, Player.P1y, 500, 100, 5, hpgain);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
    if (Player.playertemp == "Player2")
    {
    HpFont.drawTextBox (Player.P2x, Player.P2y, 500, 100, 5, hpgain);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
    if (Player.playertemp == "Player3")
    {
    HpFont.drawTextBox (Player.P3x, Player.P3y, 500, 100, 5, hpgain);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }
  else
  {
  }
}  
function ShowMpGain(mpgain)
{
RenderMap();
  if (mpgain >= 0)
  {
    if (Player.playertemp == "Player1")
    {
    MpFont.drawTextBox (Player.P1x, Player.P1y, 500, 100, 5, mpgain);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
    if (Player.playertemp == "Player2")
    {
    MpFont.drawTextBox (Player.P2x, Player.P2y, 500, 100, 5, mpgain);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
    if (Player.playertemp == "Player3")
    {
    MpFont.drawTextBox (Player.P3x, Player.P3y, 500, 100, 5, mpgain);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }
}  
